<?php
/**
 * qq登录类
 * ============================================================================
 * 版权所有 2017 福建华晟网络科技，并保留所有权利。
 * 网站地址: http://www.fldcms.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: fld
 * Date: 2018-03-22
 */
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Session;
class QqController extends Controller{
	/*
	 * 初始化操作
	 */
	public function _initialize(){
		Session::start();
	}
	//qq授权登录
	public function connectOp(){
		$qq_connect = new \auth\qq\Qqconnect();
		$qq_connect->getAuthCode();
	}
	//qq返回用户信息
	public function call_returnOp(){
		$model_member = model('Member');
		$qq_connect = new \auth\qq\Qqconnect();
		$result = $qq_connect->getUserInfo();
		$openid = $result['openid'];
		//判断qq用户是否已经存在
		$member_info = $model_member->getInfo(array('openid'=>$openid),'id,nick_name,head_imgurl,type,is_del');
		$request = Request::instance();
		if(empty($member_info)){//不存在保存qq用户信息
			$user_info = $result['user_info'];
			$data = array();
			$data['openid'] = $openid;
			$data['nick_name'] = $user_info->nickname;
			$data['gender'] = $user_info->gender;
			$data['province'] = $user_info->province;
			$data['city'] = $user_info->city;
			$data['year'] = $user_info->year;
			$data['head_imgurl'] = $user_info->figureurl_qq_1;
			$data['type'] = 1;
			$data['login_ip'] = $request->ip();
			$res = $model_member->addData($data);
			if($res !== false){
				$member_info = $model_member->getInfo(array('id'=>$res),'id,nick_name,head_imgurl,type,is_del');
			}
		}
		if($member_info['is_del'] == 0 && $member_info['type'] == 1){
			Session::set('member_id',$member_info['id']);
			Session::set('nick_name',$member_info['nick_name']);
			Session::set('head_imgurl',$member_info['head_imgurl']);
		}
		$pre_url = Session::pull('pre_url');
		if(empty($pre_url)){
			$pre_url = $request->domain();
		}
		$this->redirect($pre_url);
	}
}